package com.smart.manager.weixin.mp.repository;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.smart.manager.weixin.mp.entities.models.WeixinMpReply;
import com.smart.manager.weixin.mp.entities.models.query.QWeixinMpReply;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutNewsMessage;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

/**
 * @author Xiaxuqiu
 * @description 作用描述
 * @date 2023-05-13
 **/
@Service
public class WeixinMpReplyRepository {


    /**
     * 被关键字wxmp回复
     *
     * @param appId   应用程序id
     * @param keyword 关键字
     * @return {@link WeixinMpReply}
     */
    public WeixinMpReply getWxmpReplyByKeyWord(String appId, String keyword) {
        return new QWeixinMpReply().keyword.contains(keyword).appId.eq(appId).setMaxRows(1).order().matchType.asc().findOne();
    }


    /**
     * 根据关键字被动回复消息
     */

    public WxMpXmlOutMessage getReplyContentByKeyWord(String appId, String toUser, String fromUser, String keyword) {
        //根据回复设定的回复内容
        WeixinMpReply weixinMpReply = getWxmpReplyByKeyWord(appId, keyword);
        if (weixinMpReply != null) {
            return pkgReply(toUser, fromUser, weixinMpReply);
        }
        return null;


    }


    /**
     * 根据回复对象封装回复内容
     **/
    public WxMpXmlOutMessage pkgReply(String toUser, String fromUser, WeixinMpReply weixinMpReply) {

        if (ObjectUtil.isNull(weixinMpReply)) {
            return null;
        }


        //把回复内容格式化为JSON对象
        JSONObject contentObj = weixinMpReply.getContent();


        switch (weixinMpReply.getContentType()) {
            case TEXT:
                String text = contentObj.getString("text");
                return WxMpXmlOutMessage.TEXT().content(text).fromUser(toUser).toUser(fromUser).build();
            case IMAGE:
                String mediaId1 = contentObj.getString("mediaId");
                return WxMpXmlOutMessage.IMAGE().mediaId(mediaId1).fromUser(toUser).toUser(fromUser).build();

            case VOICE:
                String mediaId2 = contentObj.getString("mediaId");
                return WxMpXmlOutMessage.VOICE().mediaId(mediaId2).fromUser(toUser).toUser(fromUser).build();

            case VIDEO:
                String mediaId3 = contentObj.getString("mediaId");
                String mediaName = contentObj.getString("mediaName");
                String mediaDescription = contentObj.getString("mediaDescription");
                return WxMpXmlOutMessage.VIDEO().title(mediaName).mediaId(mediaId3).description(mediaDescription).fromUser(toUser).toUser(fromUser).build();

            case NEWS:
                ArrayList<WxMpXmlOutNewsMessage.Item> replyNews = new ArrayList<>();
                JSONArray jsonArray = contentObj.getJSONArray("articles");
                for (int i = 0; i < jsonArray.size(); i++) {
                    JSONObject mx = jsonArray.getJSONObject(i);
                    WxMpXmlOutNewsMessage.Item item = new WxMpXmlOutNewsMessage.Item();
                    item.setTitle(mx.getString("title"));
                    item.setDescription(mx.getString("description"));
                    item.setUrl(mx.getString("url"));
                    item.setPicUrl(mx.getString("picUrl"));
                    replyNews.add(item);
                }

                return WxMpXmlOutMessage.NEWS().articles(replyNews).fromUser(toUser).toUser(fromUser).build();
            default:
                return null;
        }


    }


    public WxMpXmlOutMessage getReplyContentById(String appId, String toUser, String fromUser, String id) {
        Long aLong = Long.valueOf(id);
        WeixinMpReply reply = new QWeixinMpReply().id.eq(aLong).appId.eq(appId).setMaxRows(1).order().matchType.asc().findOne();
        if (ObjectUtil.isNull(reply)) {
            return null;
        }


        return pkgReply(toUser, fromUser, reply);


    }

    public WxMpXmlOutMessage replyCustomText(String toUser, String fromUser, String replyText) {
        return WxMpXmlOutMessage.TEXT().content(replyText).fromUser(toUser).toUser(fromUser).build();
    }


    /**
     * 根据事件查询回复内容
     *
     * @param appId 应用程序id
     * @param event 事件
     * @return {@link WeixinMpReply}
     */
    public WeixinMpReply getWxmpReplyByEvent(String appId, String event) {
        return new QWeixinMpReply().event.eq(event).appId.eq(appId).setMaxRows(1).order().matchType.asc().findOne();


    }


    /**
     * 根据事件获取回复内容
     */

    public WxMpXmlOutMessage getReplyContentByEvent(String appId, String toUser, String fromUser, String event) {

        WeixinMpReply weixinMpReply = getWxmpReplyByEvent(appId, event);
        if (weixinMpReply != null) {
            return pkgReply(toUser, fromUser, weixinMpReply);
        }
        return null;


    }

}
